//会员支持model
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_page/pre_sale/pre_sale_provider.dart';

import '../../../hj_model/mine/excharge/dc_model.dart';

class VipSupportModel {
  DCModel? daichong;
  List<IntegralList>? integralList; //积分兑换列表
  bool? isNewUser;
  List<VipProductListModel>? list;
  List<VipProductModel>? upgradeableVipCardList;

  VipSupportModel({
    this.daichong,
    this.integralList,
    this.isNewUser,
    this.list,
    this.upgradeableVipCardList,
  });

  VipSupportModel.fromJson(Map<String, dynamic>? json) {
    daichong = json?['daichong'] != null ? DCModel.fromJson(json?['daichong']) : null;
    if (json?['integralList'] != null) {
      integralList = <IntegralList>[];
      json?['integralList'].forEach((v) {
        integralList!.add(IntegralList.fromJson(v));
      });
    }
    isNewUser = json?['isNewUser'];

    /// 升级卡
    upgradeableVipCardList = (json?['upgradeableVipCardList'] as List?)
        ?.map((e) => VipProductModel.fromJson(e['product'] ?? {})..priceDiff = e['priceDiff'])
        .toList();
    if (json?['list'] != null) {
      list = <VipProductListModel>[];
      json?['list'].forEach((v) {
        VipProductListModel vipModel = VipProductListModel.fromJson(v, daichong);
        list!.add(vipModel);
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (daichong != null) {
      data['daichong'] = daichong!.toJson();
    }
    if (integralList != null) {
      data['integralList'] = integralList!.map((v) => v.toJson()).toList();
    }
    data['isNewUser'] = isNewUser;
    if (list != null) {
      data['list'] = list!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Traders {
  String? avatar;
  int? imId;
  String? nickName;
  List<PayInfos>? payInfos;
  String? userId;
  String? welcomeMsg;

  Traders({this.avatar, this.imId, this.nickName, this.payInfos, this.userId, this.welcomeMsg});

  Traders.fromJson(Map<String, dynamic> json) {
    avatar = json['avatar'];
    imId = json['imId'];
    nickName = json['nickName'];
    if (json['payInfos'] != null) {
      payInfos = <PayInfos>[];
      json['payInfos'].forEach((v) {
        payInfos!.add(PayInfos.fromJson(v));
      });
    }
    userId = json['userId'];
    welcomeMsg = json['welcomeMsg'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['avatar'] = avatar;
    data['imId'] = imId;
    data['nickName'] = nickName;
    if (payInfos != null) {
      data['payInfos'] = payInfos!.map((v) => v.toJson()).toList();
    }
    data['userId'] = userId;
    data['welcomeMsg'] = welcomeMsg;
    return data;
  }
}

class PayInfos {
  int? payMethod;
  List<int>? payType;

  PayInfos({this.payMethod, this.payType});

  PayInfos.fromJson(Map<String, dynamic> json) {
    payMethod = json['payMethod'];
    if (json['payType'] != null) {
      payType = json['payType'].cast<int>();
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['payMethod'] = payMethod;
    data['payType'] = payType;
    return data;
  }
}

class UserInfo {
  String? gender;
  String? name;
  String? portrait;
  int? uid;

  UserInfo({this.gender, this.name, this.portrait, this.uid});

  UserInfo.fromJson(Map<String, dynamic> json) {
    gender = json['gender'];
    name = json['name'];
    portrait = json['portrait'];
    uid = json['uid'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['gender'] = gender;
    data['name'] = name;
    data['portrait'] = portrait;
    data['uid'] = uid;
    return data;
  }
}

class IntegralList {
  String? desc;
  int? duration;
  String? id;
  String? img;
  String? name;
  int? price;
  int? type;
  String? bgImg;

  IntegralList({this.desc, this.duration, this.id, this.img, this.name, this.price, this.type});

  IntegralList.fromJson(Map<String, dynamic> json) {
    desc = json['desc'];
    duration = json['duration'];
    id = json['id'];
    img = json['img'];
    name = json['name'];
    price = json['price'];
    bgImg = json['bgImg'];
    type = json['type'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['desc'] = desc;
    data['duration'] = duration;
    data['id'] = id;
    data['img'] = img;
    data['name'] = name;
    data['price'] = price;
    data['type'] = type;
    return data;
  }
}

class VipProductListModel {
  List<VipProductModel>? list;
  String? position;
  String? positionID;
  int? showType;

  VipProductListModel({this.list, this.position, this.positionID, this.showType});

  VipProductListModel.fromJson(Map<String, dynamic> json, DCModel? daichong) {
    if (json['list'] != null) {
      list = <VipProductModel>[];
      json['list'].forEach((v) {
        VipProductModel pModel = VipProductModel.fromJson(v);
        pModel.daichong = daichong;
        list!.add(pModel);
      });
    }
    position = json['position'];
    positionID = json['positionID'];
    showType = json['showType'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (list != null) {
      data['list'] = list!.map((v) => v.toJson()).toList();
    }
    data['position'] = position;
    data['positionID'] = positionID;
    data['showType'] = showType;
    return data;
  }
}

class VipProductModel {
  String? actionDesc;
  int? aiUndressCount;
  String? alias;
  String? bgImg;
  int? chanSplitMod;
  int? chatPrice;
  String? createdAt;
  String? desc;
  int? discountedPrice;
  int? discountedPriceAnd;
  int? discountedPriceIos;
  int? downloadCount;
  int? duration;
  int? everyDayGiveCoin;
  String? exclusiveOffer;
  int? giveCoin;
  int? giveFruitCoin;
  int? goldVideoCouponCount;
  int? goldVideoCouponNum;
  int? goldVideoFreeDay;
  bool? isAmountPay; // true 支持金币支付
  String? newBgImg;
  String? newName;
  List<NewPrivilege>? newPrivilege;
  int? originalPrice;
  int? payVidDiscount;
  String? position;
  List<int>? privilege;
  String? privilegeDesc;
  String? productID;
  String? productName;
  int? productType; //21- 预售卡
  List<RchgType>? rchgType;
  int? serviceTime;
  int? showCountdownTime;
  int? signDays;
  int? sort;
  bool? status;
  int? timesAWeek;
  int? type; //会员卡类型 1、会员卡 2、礼包卡
  bool? unitPriceDisplay;
  String? updatedAt;
  int? videoDiscount;
  String? vipCardDesc;
  int? vipLevel;
  String? tag;
  DCModel? daichong; // 从上级数据结构手动赋值过来
  int? priceDiff;

  int get discountedPriceUI {
    return (discountedPrice ?? 0) ~/ 10;
  } // 现价 单位角(金币)

  int? prepaidPrice;
  int get prepaidPriceUI {
    return (prepaidPrice ?? 0) ~/ 10;
  }

  int get originalPriceUI {
    return (originalPrice ?? 0) ~/ 10;
  } // 现价 单位角(金币)

  bool isPreSale = false;

  // 预售新增字段
  String? endBgImg;
  String? endBgSelectImg;

  List<RchgType> get rchgTypeUI {
    List<RchgType> payList = [];
    List<String> payNameArr = ["支付宝(人工充值)", "微信(人工充值)", "银联(人工充值)", "信用卡(人工充值)", "花呗(人工充值)", "云闪付(人工充值)", "QQ錢包(人工充值)", "京东支付(人工充值)"];
    for (RchgType rechargeTypeBean in (rchgType ?? [])) {
      if (rechargeTypeBean.type == 'daichong') {
        if (daichong?.traders?.isNotEmpty == true) {
          PayForModel dcPayModel = daichong!.traders![0];
          if (dcPayModel.payInfos?.isNotEmpty == true) {
            for (PayInfoModel payInfoModel in dcPayModel.payInfos!) {
              var payType = RchgType();
              payType.isOfficial = true;
              payType.channel = rechargeTypeBean.channel;
              payType.incrAmount = rechargeTypeBean.incrAmount;
              payType.incTax = rechargeTypeBean.incTax;
              payType.payMethod = payInfoModel.payMethod;
              if (payInfoModel.payMethod == 101) {
                payType.type = 'alipy';
                payType.typeName = payNameArr[0];
              } else if (payInfoModel.payMethod == 102) {
                payType.type = 'wechat';
                payType.typeName = payNameArr[1];
              } else if (payInfoModel.payMethod == 103) {
                payType.type = 'union';
                payType.typeName = payNameArr[2];
              } else if (payInfoModel.payMethod == 104) {
                payType.type = 'credit';
                payType.typeName = payNameArr[3];
              } else if (payInfoModel.payMethod == 105) {
                payType.type = 'huabei';
                payType.typeName = payNameArr[4];
              } else if (payInfoModel.payMethod == 106) {
                payType.type = 'yunSanPay';
                payType.typeName = payNameArr[5];
              } else if (payInfoModel.payMethod == 107) {
                payType.type = 'qqWallet';
                payType.typeName = payNameArr[6];
              } else if (payInfoModel.payMethod == 108) {
                payType.type = 'jindongPay';
                payType.typeName = payNameArr[7];
              }
              payList.add(payType);
            }
          }
        }
      } else {
        payList.add(rechargeTypeBean);
      }
    }
    if (isAmountPay == true) {
      RchgType coinType = RchgType();
      coinType.type = "coin";
      coinType.typeName = "金币";
      payList.add(coinType);
    }

    return payList;
  }

  VipProductModel({
    this.actionDesc,
    this.aiUndressCount,
    this.alias,
    this.bgImg,
    this.chanSplitMod,
    this.chatPrice,
    this.createdAt,
    this.desc,
    this.discountedPrice,
    this.discountedPriceAnd,
    this.discountedPriceIos,
    this.downloadCount,
    this.duration,
    this.everyDayGiveCoin,
    this.exclusiveOffer,
    this.giveCoin,
    this.giveFruitCoin,
    this.goldVideoCouponCount,
    this.goldVideoCouponNum,
    this.goldVideoFreeDay,
    this.isAmountPay,
    this.newBgImg,
    this.newName,
    this.newPrivilege,
    this.originalPrice,
    this.payVidDiscount,
    this.position,
    this.privilege,
    this.privilegeDesc,
    this.productID,
    this.productName,
    this.productType,
    this.rchgType,
    this.serviceTime,
    this.showCountdownTime,
    this.signDays,
    this.sort,
    this.status,
    this.timesAWeek,
    this.type,
    this.unitPriceDisplay,
    this.updatedAt,
    this.videoDiscount,
    this.vipCardDesc,
    this.vipLevel,
    this.isPreSale = false,
    this.tag,
    this.priceDiff,
  });

  VipProductModel.fromJson(Map<String, dynamic> json) {
    prepaidPrice = json['prepaidPrice'];
    endBgImg = json['endBgImg'];
    endBgSelectImg = json['endBgSelectImg'];
    actionDesc = json['actionDesc'];
    aiUndressCount = json['aiUndressCount'];
    alias = json['alias'];
    bgImg = json['bgImg'];
    chanSplitMod = json['chanSplitMod'];
    chatPrice = json['chatPrice'];
    createdAt = json['createdAt'];
    desc = json['desc'];
    discountedPrice = json['discountedPrice'];
    discountedPriceAnd = json['discountedPriceAnd'];
    discountedPriceIos = json['discountedPriceIos'];
    downloadCount = json['downloadCount'];
    duration = json['duration'];
    everyDayGiveCoin = json['everyDayGiveCoin'];
    exclusiveOffer = json['exclusiveOffer'];
    giveCoin = json['giveCoin'];
    giveFruitCoin = json['giveFruitCoin'];
    goldVideoCouponCount = json['goldVideoCouponCount'];
    goldVideoCouponNum = json['goldVideoCouponNum'];
    goldVideoFreeDay = json['goldVideoFreeDay'];
    isAmountPay = json['isAmountPay'];
    newBgImg = json['newBgImg'];
    newName = json['newName'];
    tag = json['tag'];
    if (json['newPrivilege'] != null) {
      newPrivilege = <NewPrivilege>[];
      json['newPrivilege'].forEach((v) {
        newPrivilege!.add(NewPrivilege.fromJson(v));
      });
    }
    originalPrice = json['originalPrice'];
    payVidDiscount = json['payVidDiscount'];
    position = json['position'];
    if (json['privilege'] != null) {
      privilege = (json['privilege'] as List?)?.map((v) {
        return int.tryParse(v.toString()) ?? 0;
      }).toList();
    }
    privilegeDesc = json['privilegeDesc'];
    productID = json['productID'];
    productName = json['productName'];
    productType = json['productType'];
    if (json['rchgType'] != null) {
      rchgType = <RchgType>[];
      json['rchgType'].forEach((v) {
        rchgType!.add(RchgType.fromJson(v));
      });
    }
    serviceTime = json['serviceTime'];
    showCountdownTime = json['showCountdownTime'];
    signDays = json['signDays'];
    sort = json['sort'];
    status = json['status'];
    timesAWeek = json['timesAWeek'];
    type = json['type'];
    unitPriceDisplay = json['unitPriceDisplay'];
    updatedAt = json['updatedAt'];
    videoDiscount = json['videoDiscount'];
    vipCardDesc = json['vipCardDesc'];
    vipLevel = json['vipLevel'];
    isPreSale = false;
  }

  //是否有折扣
  bool hasDiscout() {
    return discountedPrice != originalPrice;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['actionDesc'] = actionDesc;
    data['aiUndressCount'] = aiUndressCount;
    data['alias'] = alias;
    data['bgImg'] = bgImg;
    data['chanSplitMod'] = chanSplitMod;
    data['chatPrice'] = chatPrice;
    data['createdAt'] = createdAt;
    data['desc'] = desc;
    data['discountedPrice'] = discountedPrice;
    data['discountedPriceAnd'] = discountedPriceAnd;
    data['discountedPriceIos'] = discountedPriceIos;
    data['downloadCount'] = downloadCount;
    data['duration'] = duration;
    data['everyDayGiveCoin'] = everyDayGiveCoin;
    data['exclusiveOffer'] = exclusiveOffer;
    data['giveCoin'] = giveCoin;
    data['giveFruitCoin'] = giveFruitCoin;
    data['goldVideoCouponCount'] = goldVideoCouponCount;
    data['goldVideoCouponNum'] = goldVideoCouponNum;
    data['goldVideoFreeDay'] = goldVideoFreeDay;
    data['isAmountPay'] = isAmountPay;
    data['newBgImg'] = newBgImg;
    data['newName'] = newName;
    if (newPrivilege != null) {
      data['newPrivilege'] = newPrivilege!.map((v) => v.toJson()).toList();
    }
    data['originalPrice'] = originalPrice;
    data['payVidDiscount'] = payVidDiscount;
    data['position'] = position;
    if (privilege != null) {
      data['privilege'] = privilege!.map((v) => v).toList();
    }
    data['privilegeDesc'] = privilegeDesc;
    data['productID'] = productID;
    data['productName'] = productName;
    data['productType'] = productType;
    if (rchgType != null) {
      data['rchgType'] = rchgType!.map((v) => v.toJson()).toList();
    }
    data['serviceTime'] = serviceTime;
    data['showCountdownTime'] = showCountdownTime;
    data['signDays'] = signDays;
    data['sort'] = sort;
    data['status'] = status;
    data['timesAWeek'] = timesAWeek;
    data['type'] = type;
    data['unitPriceDisplay'] = unitPriceDisplay;
    data['updatedAt'] = updatedAt;
    data['videoDiscount'] = videoDiscount;
    data['vipCardDesc'] = vipCardDesc;
    data['vipLevel'] = vipLevel;
    return data;
  }
}

extension PreSale on VipProductModel {
  String realNormalVipImage() {
    if (!isPreSale) return bgImg ?? '';
    if (PreSaleProvider().canPayBalance()) return endBgImg ?? '';
    return bgImg ?? '';
  }

  String realSelectVipImage() {
    if (!isPreSale) return newBgImg ?? '';
    if (PreSaleProvider().canPayBalance()) return endBgSelectImg ?? '';
    return newBgImg ?? '';
  }
}

class NewPrivilege {
  String? id;
  String? img;
  String? privilegeDesc;
  String? privilegeName;
  int? privilege;
  String? uncheckedImg;

  NewPrivilege({this.img, this.privilegeDesc, this.privilegeName});

  NewPrivilege.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    img = json['img'];
    privilegeDesc = json['privilegeDesc'];
    privilegeName = json['privilegeName'];
    privilege = json['privilege'];
    uncheckedImg = json['uncheckedImg'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['img'] = img;
    data['privilegeDesc'] = privilegeDesc;
    data['privilegeName'] = privilegeName;
    data['uncheckedImg'] = uncheckedImg;
    data['privilege'] = privilege;
    return data;
  }
}

class RchgType {
  String? channel; //渠道类型 鲨鱼 金鱼
  int? incTax; //按比率增加额外优惠额 0-1之间 ,如果 incrAmount 与 incrTax 同时存在 以 incrAmount 为准
  int? incrAmount; //增加的优惠额度
  String? type; //充值方式  //coin 金币方式
  String? typeName; //支付宝，微信，银联

  //daichong 业务字段
  bool? isOfficial = false; // 官方推荐
  int? payMethod;

  RchgType({this.channel, this.incTax, this.incrAmount, this.type, this.typeName});

  RchgType.fromJson(Map<String, dynamic> json) {
    channel = json['channel'];
    incTax = json['incTax'];
    incrAmount = json['incrAmount'];
    type = json['type'];
    typeName = json['typeName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['channel'] = channel;
    data['incTax'] = incTax;
    data['incrAmount'] = incrAmount;
    data['type'] = type;
    data['typeName'] = typeName;
    return data;
  }

  String getPayIcon() {
    if (isOfficial == true) {
      // 代充
      return "pay_icon103.png".mineImgPath;
    } else if (type == 'alipay') {
      return "ic_alipay.png".mineImgPath;
    } else if (type == 'union') {
      return "pay_icon103.png".mineImgPath;
    } else if (type == 'wechat') {
      return "ic_wechat.png".mineImgPath; //AssetsSvg.SVG_PAY_ICON102;
    } else if (type == 'coin') {
      //金币
      return "ic_coin.png".mineImgPath; //AssetsSvg.SVG_PAY_ICON102;
    } else if (type == 'usdt') {
      return "assets/images/pay_icon/pay_icon105.png";
    } else {
      return "ic_coupon.png".mineImgPath;
    }
  }
}

/// 加赠券

class CouponModel {
  String? cId;
  int? count;
  String? createTime;
  String? expireTime;
  String? id;
  String? name;
  int? price;
  int? type;
  bool? used;
  int? value;

  CouponModel({this.cId, this.count, this.createTime, this.expireTime, this.id, this.name, this.price, this.type, this.used, this.value});

  CouponModel.fromJson(Map<String, dynamic> json) {
    cId = json['cId'];
    count = json['count'];
    createTime = json['createTime'];
    expireTime = json['expireTime'];
    id = json['id'];
    name = json['name'];
    price = json['price'];
    type = json['type'];
    used = json['used'];
    value = json['value'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['cId'] = this.cId;
    data['count'] = this.count;
    data['createTime'] = this.createTime;
    data['expireTime'] = this.expireTime;
    data['id'] = this.id;
    data['name'] = this.name;
    data['price'] = this.price;
    data['type'] = this.type;
    data['used'] = this.used;
    data['value'] = this.value;
    return data;
  }
}
